
import controller.Controller;

import model.ISPGraph;
import model.SPGraph;

import view.GraphicalView;
import view.TextualView;
import view.WindowView;

public class Main {

	/**
	 * Runs examples of SPGraph constructions 
	 * @param args
	 */
	public static void main(String[] args) {
		
		ISPGraph spGraph = new SPGraph();
		
		WindowView wView = createWindowView();
		GraphicalView gView = createGraphicalView(spGraph);
		TextualView tView = createTextualView(spGraph);
		
		assignController(wView, gView, tView);
		wView.display();
		
	}

	private static WindowView createWindowView() {
		return new WindowView();
	}
	
	private static GraphicalView createGraphicalView(ISPGraph model) {
		if (model == null) {
			throw new IllegalArgumentException("model argument is null");
		}
		return new GraphicalView(model);
	}
	
	private static TextualView createTextualView(ISPGraph model) {
		if (model == null) {
			throw new IllegalArgumentException("model argument is null");
		}
		return new TextualView(model);
	}
	
	private static Controller createController(WindowView wView,
			GraphicalView gView, TextualView tView) {
		if (wView == null) {
			throw new IllegalArgumentException("window view argument is null");
		}
		if (gView == null) {
			throw new IllegalArgumentException("graphical view argument is null");
		}
		if (tView == null) {
			throw new IllegalArgumentException("textual view argument is null");
		}
		return new Controller(wView, gView, tView);
	}
	
	private static void assignController(WindowView wView,
			GraphicalView gView, TextualView tView) {
		if (wView == null) {
			throw new IllegalArgumentException("window view argument is null");
		}
		if (gView == null) {
			throw new IllegalArgumentException("graphical view argument is null");
		}
		if (tView == null) {
			throw new IllegalArgumentException("textual view argument is null");
		}
		Controller controller = createController(wView, gView, tView);
		
		SPGraph model = (SPGraph) (gView.getModel());
		model.addObserver(controller.getGraphObserver());
		
		gView.addMouseListener(controller.getGraphicalMouseListener());
		gView.addMouseMotionListener(controller.getGraphicalMouseMotionListener());
		
		wView.setGraphView(gView);
		
		wView.getFrame().addWindowListener(controller.getFrameListener());
		wView.getSubmitButton().addActionListener(controller.getSubmitButtonListener());
		wView.getGraphicalViewButton().addActionListener(controller.getGraphicalViewListener());
		wView.getTextualViewButton().addActionListener(controller.getTextualViewListener());
		wView.getSaveItem().addActionListener(controller.getSaveItemListener());
		wView.getOpenItem().addActionListener(controller.getOpenItemListener());
		wView.getNewItem().addActionListener(controller.getNewItemListener());
		wView.getDecodeButton().addActionListener(controller.getDecodeButtonListener());
	}
}
